use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ApplicationConfig {
    pub debug: bool,
    pub server_url: String,
    pub port: u16,
    pub db_url: String,
    pub db_pool_len: usize,
    pub db_pool_timeout: usize,
    pub redis_url: String,
    pub jwt_secret: String,
    pub jwt_exp: usize,
    pub exclude_path: Option<Vec<String>>,
}

impl Default for ApplicationConfig {
    fn default() -> Self {
        let yml = include_str!("../../config.yml");
        let config: ApplicationConfig =
            serde_yaml::from_str(&yml).expect("Parser config file faild!");
        config
    }
}

impl ApplicationConfig {
    /// 传入一个路径，判断是否需要验证
    pub fn is_exclude(&self, path: &str) -> bool {
        if let Some(exclude_path) = &self.exclude_path {
            for p in exclude_path {
                // 不接受模糊匹配
                if p == path {
                    return true;
                }
            }
        }
        // 接受模糊匹配
        if let Some(exclude_path) = &self.exclude_path {
            for p in exclude_path {
                if p.contains("*") {
                    let p = p.replace("*", "");
                    if path.starts_with(p.as_str()) {
                        return true;
                    }
                }
            }
        }
        false
    }
}
